<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">

        <p-row>
		  <p-col  :span="24">
			<p-form-item label="补印份数" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input-number   v-decorator="[ 'qty', validatorRules.qty]" placeholder="请输入补印份数" style="width: 100%"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="标签模板编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<f-popup
						contextPath="mes-code"
						v-decorator="['labelTemplateNo', validatorRules.labelTemplateNo]"
						:trigger-change="true"
						org-fields="id,label_template_no,label_template_name,dptname"
						dest-fields="label_template_id,label_template_no,label_template_name,dptname"
						code="mes_get_barcode_template"
						@callback="popupCallback"/>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="标签模板名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'labelTemplateName', validatorRules.labelTemplateName]" placeholder="请输入标签模板名称"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="责任部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input readOnly v-decorator="[ 'dptname', validatorRules.dptname]" placeholder="请输入责任部门"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="SN工站扫描" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'processNo', validatorRules.processNo]" placeholder="请输入SN工站扫描"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="外箱条码" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'packBarcode', validatorRules.packBarcode]" placeholder="请输入外箱条码"></p-input>
			</p-form-item>
		  </p-col>
		  <p-col  :span="24">
			<p-form-item label="补印SN" :labelCol="labelCol" :wrapperCol="wrapperCol">
				<p-input  v-decorator="[ 'barcode', validatorRules.barcode]" placeholder="请输入补印SN"></p-input>
			</p-form-item>
		  </p-col>
        </p-row>


      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "MesBcPrintSnModal",
    mixins: [SubmitOnchangMixin],
    components: {
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          qty: {rules: [
            {required: true, message: '请输入补印份数!'},
            {pattern:/^-?\d+$/, message: '请输入整数!'},
          ]},
          labelTemplateNo: {rules: [
            {required: true, message: '请输入标签模板编码!'},
          ]},
          labelTemplateName: {rules: [
          ]},
          dptname: {rules: [
          ]},
          processNo: {rules: [
            {required: true, message: '请输入SN工站扫描!'},
          ]},
          packBarcode: {rules: [
            {required: true, message: '请输入外箱条码!'},
          ]},
          barcode: {rules: [
            {required: true, message: '请输入补印SN!'},
          ]},
        },
        url: {
          add: '/mes-code/auto/mesBcPrintSn/add',
          edit: '/mes-code/auto/mesBcPrintSn/edit',
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },

      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'qty','labelTemplateNo','labelTemplateName','dptname','processNo','packBarcode','barcode'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'qty','labelTemplateNo','labelTemplateName','dptname','processNo','packBarcode','barcode'))
      },


    }
  }
</script>